<?php

namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Models\Agent;

class AgentAuth
{
    public function handle(Request $req, Closure $next):Response
    {
        if (strpos($req->header('referer'), 'localhost') > 0) {
            $req->agent = Agent::find(5);
            return $next($req);
        }
        $cannologin = strpos($req->path(), 'agent/level') > 0 || $req->path() == 'api/agent';
        if ($req->hasCookie('superbar_agenttoken')) {
            $req->agent = Agent::where('token',base64_decode($req->cookie('superbar_agenttoken')))->first();
            if (empty($req->agent) && !$cannologin) wxresp(-1);
        }elseif (!$cannologin) wxresp(-1);
        return $next($req);
    }
}